package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品管理接口")
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

//   分页查询菜品列表
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品列表，参数：{}", dishPageQueryDTO);
     PageResult results = dishService.pageDish(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize(),
             dishPageQueryDTO.getName(), dishPageQueryDTO.getCategoryId(), dishPageQueryDTO.getStatus());
      return Result.success(results);
    }

    //新增菜品
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品，参数：{}", dishDTO);
        dishService.save(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        clearCacheData(key);
        return Result.success();
    }

    //根据分类id查询菜品+口味信息
    @GetMapping("/list")
    public Result<List<DishVO>> list( Long categoryId) {
        log.info("根据分类id查询菜品信息，分类id为{}", categoryId);
        Dish dish = new Dish();
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCategoryId(categoryId);
        List<DishVO> dishVOList = dishService.listWithFlavor(dish);


        return Result.success(dishVOList);
    }

    // 查询指定分类下的菜品在新增套餐时查询并选择
    @GetMapping("/list/{categoryId}")
    public Result<List<DishVO>> list2(@PathVariable Long categoryId) {
        log.info("根据分类id查询菜品信息，分类id为{}", categoryId);
        Dish dish = new Dish();
        dish.setStatus(StatusConstant.ENABLE);
        dish.setCategoryId(categoryId);
        List<DishVO> dishVOList = dishService.listWithFlavor(dish);
        return Result.success(dishVOList);
    }

    //删除菜品信息
    @DeleteMapping
    public Result delete(@RequestParam List<String> ids){
        log.info("删除菜品信息，ids：{}", ids);
        dishService.delete(ids);

        //清理缓存数据 清理所有菜品缓存
        clearCacheData("dish_*");
        return Result.success();


    }

    //根据id查询菜品信息
    @GetMapping("/{id}")
    public Result<DishVO> getById(Dish dish){
        Dish dishById = new Dish();
        dishById.setId(dish.getId());
        dish.setStatus(StatusConstant.ENABLE);
        DishVO result = dishService.getById(dishById);
        return Result.success(result);
    }

    //修改菜品信息
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品信息：{}", dishDTO);
        dishService.update(dishDTO);
        //清理缓存数据 清理所有菜品缓存
        clearCacheData("dish_*");
        return Result.success();
    }

    //起售停售
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable String status,@RequestParam String id) {
        //设置状态
        dishService.setStatus(status,id);
        //清理缓存数据 清理所有菜品缓存
        clearCacheData("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存数据
     */
    private void clearCacheData(String pattern) {
        //清理缓存数据 清理所有菜品缓存
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }


}
